package com.boss.ccs.gis;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import com.boss.core.Boss;
import com.boss.core.BossConstants;
import com.boss.core.BossDescriptor;
import com.boss.core.form.BossFormHelper;
import com.boss.core.manage.BossProperty;
import com.boss.core.manage.PropertySection;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import com.boss.Messages;

import hudson.Extension;
import hudson.util.ListBoxModel;

/**
 * This class supports the GIS view for Online-Help tasks.
 * @author Momin Noor Khan
 */

@Extension
public class BossGIS2 extends Boss {
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl) super.getDescriptor();
    }

    @Override
    public String getDescription() {
        return "";
    }

    /**
     * Get the project name of this order form
     * @return
     */
    public String getProjectName() {
        return BossConstants.PROJECT_GIS;
    }

    /**
     * Check if the current user is authorized for GIS support tasks
     * @return
     */
    public static boolean isAuthorizedGISUser() {
        return Boss.isAuthorizedGISUser();
    }

    /**
     * Get the display label of this form
     * @return
     */
    public String getLabel() {
        return "Online Help Form";
    }

    /**
     * Get the parent tab name of this view
     * @return
     */
    public String getTabView() {
        return BossConstants.TAB_GIS;
    }

    @Override
    public List<String> getTasks(String project) {
        return BossUtil.getPropertyValues2(project, "ONLINE-HELP-TASKS", '\n', true, false);
    }

    //To-Do: find out if it is still in use or not
    public String getTaskCopyBuild() {
        return BossConstants.TASK_COPY_BUILD;
    }

    /**
     * Gets the dummy value to be populated in the drop down as default.
     */
    //To-Do: find out if it is still in use or not
    public String getTaskDummy() {
        return BossConstants.DUMMY_VALUE;
    }

    public void doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
        JSONObject jsonForm = req.getSubmittedForm();
        String project = BossConstants.ONLINE_HELP_KEY;
        BossGIS2Form form = new BossGIS2Form(req, jsonForm, project);

        if (form.getJsonTaskSection() == null || StringUtils.isBlank(form.getTask())) {
            BossFormHelper.setFailureMsg(req, Messages.Boss_MissingTaskSection());
            req.getView(this, getPostSubmitView()).forward(req, rsp);
            return;
        }

        if (form.isValid()) {
            form.submit();
        }
        //Display the post-submit landing page
        req.getView(this, getPostSubmitView()).forward(req, rsp);
    }

    @Extension
    public static final class DescriptorImpl extends BossDescriptor {
        /**
         * Gets the list of online help artifacts from the boss config
         * @return
         */
        public List<BossProperty> getHelpArtifacts() {
            PropertySection propertySection = BossGlobalUtil.getPropertySection(BossConstants.ONLINE_HELP_ARTIFACTS_KEY);
            List<BossProperty> properties = propertySection.getProperties();
            return properties;
        }

        /**
         * Get the target location of the online help artifacts on the GIS server
         * @param server
         * @return
         */
        public String getTargetHelpLocation(String server) {
            //get the profile of the of the selected gis server
            String gisProfile = BossGlobalUtil.getGisProfilesPath() + server + ".properties";
            //from the default gis profile get the value of server home used as root
            String targetLocation = BossUtil.getPropertyValue(BossConstants.GIS_ONLINE_HELP_HOME_KEY, gisProfile);
            return targetLocation;
        }

        /**
         * Populates the GIS release drop down on the form from the properties file.
         * @return
         */
        public ListBoxModel doFillReleaseItems() {
            return getListBoxModel(BossConstants.PROJECT_CPM, BossConstants.ONLINE_HELP_RELEASES_KEY);
        }

        /**
         * Populates the customer drop down in sub-section of online help checkbox,
         * from the boss configuration based on the constant 'customer key'.
         * @return
         */
        public ListBoxModel doFillCustomerItems() {
            return getListBoxModel(BossConstants.PROJECT_CPM, BossConstants.ONLINE_HELP_CUSTOMER_KEY, false, true);
        }

        /**
         * Populates the language drop down in sub-section of online-help checkbox, from the boss configuration
         * @return
         */
        public ListBoxModel doFillLanguageItems() {
            return getListBoxModel(BossConstants.PROJECT_CPM, BossConstants.ONLINE_HELP_LANGUAGE_KEY, false, true);
        }
    }
}
